package com.beiing.tianshuai.tianshuai.mine.ui;

import android.content.ClipData;
import android.content.ClipboardManager;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageButton;
import android.widget.TextView;

import com.beiing.tianshuai.tianshuai.R;
import com.beiing.tianshuai.tianshuai.adapter.OrderTransportAdapter;
import com.beiing.tianshuai.tianshuai.app.HttpRequestConstant;
import com.beiing.tianshuai.tianshuai.base.BaseActivity;
import com.beiing.tianshuai.tianshuai.mine.entity.OrderTransportDataBean;
import com.beiing.tianshuai.tianshuai.mine.presenter.OrderTransportPresenter;
import com.beiing.tianshuai.tianshuai.mine.view.OrderTransportViewImpl;
import com.beiing.tianshuai.tianshuai.widget.CustomEmptyLayout;
import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.request.RequestOptions;
import com.netease.nim.uikit.common.util.log.LogUtil;
import com.qmuiteam.qmui.widget.QMUIRadiusImageView;

import java.text.MessageFormat;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

public class OrderTransportActivity extends BaseActivity implements OrderTransportViewImpl {

    /**
     * //toolbar返回按钮
     */
    @BindView(R.id.toolbar_navigation_back)
    ImageButton toolbarNavigation;

    /**
     * //toolbar更多信息按钮
     */
    @BindView(R.id.toolbar_filter_more)
    ImageButton toolbarFilter;

    /**
     * //toolbar更多信息按钮
     */
    @BindView(R.id.iv_order_photo)
    QMUIRadiusImageView ivOrderPhoto;

    /**
     * toolbar更多信息按钮
     */
    @BindView(R.id.tv_order_name)
    TextView tvOrderName;

    /**
     *    //复制快递单编号按钮
     */
    @BindView(R.id.tv_order_number_cope)
    TextView tvOrderNumberCope;

    /**
     * //快递单编号
     */
    @BindView(R.id.tv_order_number)
    TextView tvOrderNumber;

    /**
     *    订单状态 发货

     */
    @BindView(R.id.tv_order_transport_start)
    TextView tvOrderTransportStart;

    /**
     *    订单状态 运输
     */
    @BindView(R.id.tv_order_transport_going)
    TextView tvOrderTransportGoing;

    /**
     *     订单状态 派件
     */
    @BindView(R.id.tv_order_transport_get_to_home)
    TextView tvOrderTransportGetToHome;

    /**
     *     订单状态 签收
     */
    @BindView(R.id.tv_order_transport_sign_get)
    TextView tvOrderTransportSignGet;

    /**
     *     订单状态 发货图标
     */

    @BindView(R.id.qr_iv_order_transport_start_ic)
    QMUIRadiusImageView qrIvOrderTransportStartIc;

    /**
     * //订单状态 运输图标
     */
    @BindView(R.id.qr_iv_order_transport_going_ic)
    QMUIRadiusImageView qrIvOrderTransportGoingIc;
    /**
     * 订单状态 派件图标
     */
    @BindView(R.id.qr_iv_order_transport_get_to_home_ic)
    QMUIRadiusImageView qrIvOrderTransportGetToHomeIc;
    /**
     * 订单状态 签收图标
     */
    @BindView(R.id.qr_iv_order_transport_sign_get_ic)
    QMUIRadiusImageView qrIvOrderTransportSignGetIc;

    /**
     * 订单发货地
     */
    @BindView(R.id.tv_order_transport_from)
    TextView tvOrderTransportFrom;
    /**
     * 订单目的地
     */
    @BindView(R.id.tv_order_transport_go)
    TextView tvOrderTransportGo;

    /**
     * 联系卖家按钮
     */
    @BindView(R.id.tv_order_chat_seller)
    TextView tvOrderChatSeller;
    /**
     * 非票务类照片比例参数
     */
    @BindView(R.id.cel_empty_view_order_transport)
    CustomEmptyLayout mCustomEmptyLayout;

    private OrderTransportPresenter mPresenter;

    public static void start(Context context, String orderId) {
        Intent intent = new Intent(context, OrderTransportActivity.class);
        intent.putExtra("orderStatus_id", orderId);
        context.startActivity(intent);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_order_transport;
    }

    @Override
    protected void init() {
        showShortText(getOrderStatusId());
        mCustomEmptyLayout.setInProgress();
        initPresenter();
    }

    /**
     * 要请求的订单号
     */
    public void initPresenter() {
        mPresenter = new OrderTransportPresenter(this);
        mPresenter.getOrderStatus(getOrderStatusId());
    }

    /**
     *      获取的id
     */
    private String getOrderStatusId() {
        Intent intent = getIntent();
        return intent.getStringExtra("orderStatus_id");
    }

    /**
     * 网络请求开始展示
     */
    @Override
    public void showProgress() {
        //过度视图显示
        if (mCustomEmptyLayout != null) {
            mCustomEmptyLayout.setInProgress();
        }
    }

    /**
     * 网络请求结束
     */
    @Override
    public void hideProgress() {
        //隐藏过度视图，过度视图有很多层，如果调用setChildrenVisible不能判断the number of children in the group为空
        if (mCustomEmptyLayout != null) {
            //没有设置
            mCustomEmptyLayout.setChildrenVisible();
        }
    }

    /**
     * 请求订单物流成功
     *
     * @param bean 物流信息
     */
    @Override
    public void onRequestSuccess(OrderTransportDataBean bean) {
        int code = bean.getCode();
        switch (code) {
            case 200:
                final OrderTransportDataBean.DataBean data = bean.getData();
                List<OrderTransportDataBean.DataBean.NumberBean.ListBean> mList = data.getNumber().getList();
                //初始化RecyclerView
                RecyclerView mRecyclerView = findViewById(R.id.rlv_order_transport);
                //创建LinearLayoutManager 对象
                LinearLayoutManager mLayoutManager = new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false);
                //设置RecyclerView 布局
                mRecyclerView.setLayoutManager(mLayoutManager);
                //设置Adapter
                OrderTransportAdapter adapter = new OrderTransportAdapter(mContext, mList);
                mRecyclerView.setAdapter(adapter);

                tvOrderNumberCope.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        //获取剪贴板管理器：
                        ClipboardManager cm = (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE);
                        // 创建普通字符型ClipData
                        ClipData mClipData = ClipData.newPlainText("label1", tvOrderNumber.getText());
                        // 将ClipData内容放到系统剪贴板里。
                        if (cm != null) {
                            cm.setPrimaryClip(mClipData);
                            showShortText("订单编号已复制");
                        } else {
                            showShortText("没有复制");
                        }
                    }
                });
                //获取订单状态赋值给mOrderState,
                String mOrderState = data.getNumber().getDeliverystatus();
                //根据不同的状态设置横向物流状态控件的进度显示
                //统一设置蓝色背景
                Drawable mTextDrawable = ContextCompat.getDrawable(mContext, R.drawable.ic_rectangle_blue);
                Drawable mQrImageDrawable = getResources().getDrawable(R.drawable.ic_oval_blue);
                switch (mOrderState) {
                    case "0":
                        tvOrderTransportStart.setBackground(mTextDrawable);
                        tvOrderTransportStart.setTextColor(Color.WHITE);
                        qrIvOrderTransportStartIc.setImageDrawable(mQrImageDrawable);
                        break;
                    case "1":
                        tvOrderTransportGoing.setBackground(mTextDrawable);
                        tvOrderTransportGoing.setTextColor(Color.WHITE);
                        qrIvOrderTransportGoingIc.setImageDrawable(mQrImageDrawable);
                        break;
                    case "2":
                        tvOrderTransportGetToHome.setBackground(mTextDrawable);
                        tvOrderTransportGetToHome.setTextColor(Color.WHITE);
                        qrIvOrderTransportGetToHomeIc.setImageDrawable(mQrImageDrawable);
                        break;
                    case "3":
                        tvOrderTransportSignGet.setBackground(mTextDrawable);
                        tvOrderTransportSignGet.setTextColor(Color.WHITE);
                        qrIvOrderTransportSignGetIc.setImageDrawable(mQrImageDrawable);
                        break;
                    default:
                        break;
                }
                //设置商品出发地和目的地
                tvOrderTransportGo.setText(data.getNumber().getMerch());
                tvOrderTransportFrom.setText(data.getNumber().getUserAddress());
                //设置商品名称和物流编号
                tvOrderNumber.setText(MessageFormat.format("{0}：{1}", data.getNumber().getCompany(), data.getNumber().getNumber()));
                tvOrderName.setText(data.getNumber().getAname());
                if (data.getNumber().getCover() == null) {
                    showShortText("货物图片为空" + getOrderStatusId());
                    LogUtil.i("===============", "图片为空" + getOrderStatusId());
                }
                //通过设置图片比例通过glide设置商品图片
                //非票务类照片比例参数
                RequestOptions goodsPhotoOptions = new RequestOptions()
                        .diskCacheStrategy(DiskCacheStrategy.ALL)
                        .centerCrop()
                        .placeholder(R.drawable.default_avatar)
                        .override(90, 90);

                Glide.with(mContext.getApplicationContext())
                        .load(HttpRequestConstant.ACTIVITY_PIC_HEAD + data.getNumber().getCover())
                        .apply(goodsPhotoOptions)
                        .into(ivOrderPhoto);

                //联系卖家按钮点击事件
                tvOrderChatSeller.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        showShortText("联系卖家接口");
                    }
                });
                break;
            default:
                showShortText("未知参数" + code);
                LogUtil.e("OrderTransport", "" + code);
                break;
        }
    }

    @Override
    public void onRequestFailed(Throwable throwable) {

    }

    @Override
    public void getOrderInformationSuccess(OrderTransportDataBean bean) {

    }

    @Override
    public void getOrderInformationError(OrderTransportDataBean bean) {

    }

    @OnClick(R.id.toolbar_navigation_back)
    public void onToolbarNavigationClicked() {
        finish();
    }

    @OnClick(R.id.toolbar_filter_more)
    public void onToolbarFilterClicked() {
        showShortText("更多信息");
    }
}
